const fs = require("fs");
const path = require("path");

const buf = Buffer.alloc(10);
const BUFFER_SIZE = buf.length;
let readoffset = 0;

fs.open(path.resolve("a.txt"), "r", (_err, rfd) => {
  fs.open(path.resolve("b.txt"), "w", (_err, wfd) => {
    function next() {
      fs.read(rfd, buf, 0, BUFFER_SIZE, readoffset, (_rErr, readBytes) => {
        if (!readBytes) {
          fs.close(rfd, () => {});
          fs.close(wfd, () => {});
          console.log("拷贝完成");
          return;
        }
        readoffset += readBytes;
        fs.write(wfd, buf, 0, readBytes, (_wErr, _written) => {
          next();
        });
      });
    }

    next();
  });
});
